package cn.com.dwr.modules.app.controller;

import cn.com.dwr.common.lang.Result;
import cn.com.dwr.entity.AppProduct;
import cn.com.dwr.service.AppProductService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/app")
public class ProductController extends BaseController{

    @Resource
    AppProductService appProductService;

    /**
     * 商品分类
     * @param categoryId 分类id
     * @param sort 排序方式：1-默认排序，2-最新上市，3-销量最多
     * @return
     */
    @GetMapping("/category/{categoryId}")
    public Result category(@PathVariable Long categoryId,
                           @RequestParam(defaultValue = "0") Integer sort){
        return Result.success(appProductService.pageByCategoryId(getPage(), categoryId, sort));
    }

    /**
     * 商品详情
     * @param productId
     * @return
     */
    @GetMapping("/product/{productId}")
    public Result product(@PathVariable Long productId){
        AppProduct product = appProductService.getWithAttrsById(productId);
        if(!product.getIsOnSale()){
            return Result.fail("商品已下架");
        }
        return Result.success(product);
    }
}
